package gui.selectteam;

import gui.TradecisionbaMainWindow;
import gui.TradecisionbaMainWindow.SelectTeamActionListener;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;

import javax.swing.DefaultCellEditor;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.table.TableCellRenderer;

import commondata.ITeam;
import commondata.NbaPool;
import java.awt.Color;

/**
 * The Class SelectTeamPanel.
 */
public class SelectTeamPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7571995900957075146L;

	/** The select team label. */
	private JLabel selectTeamLabel = null;

	/** The teams table. */
	private JTable teamsTable = null;

	/** The teams scroll pane. */
	private JScrollPane teamsScrollPane = null;

	/** The select team table model. */
	private SelectTeamTableModel selectTeamTableModel = null; // @jve:decl-index=0:visual-constraint="223,400"

	/** The main window. */
	private TradecisionbaMainWindow mainWindow;

	/**
	 * This method initializes.
	 */
	public SelectTeamPanel() {
		super();
		initialize();
	}

	/**
	 * This method initializes this.
	 */
	private void initialize() {
		GridBagConstraints gridBagConstraints21 = new GridBagConstraints();
		gridBagConstraints21.fill = GridBagConstraints.BOTH;
		gridBagConstraints21.weighty = 1.0;
		gridBagConstraints21.gridx = 0;
		gridBagConstraints21.gridy = 1;
		gridBagConstraints21.insets = new Insets(0, 20, 0, 20);
		gridBagConstraints21.weightx = 1.0;
		GridBagConstraints gridBagConstraints20 = new GridBagConstraints();
		gridBagConstraints20.fill = GridBagConstraints.BOTH;
		gridBagConstraints20.weighty = 0.0D;
		gridBagConstraints20.gridx = 0;
		gridBagConstraints20.gridy = 0;
		gridBagConstraints20.insets = new Insets(20, 20, 20, 20);
		gridBagConstraints20.weightx = 0.0D;
		selectTeamLabel = new JLabel();
		selectTeamLabel.setText("SELECT A TEAM FOR TRADE ADVICE");
		selectTeamLabel
				.setFont(new Font("Dialog", Font.BOLD | Font.ITALIC, 14));
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		screenSize.height -= 30;
		this.setSize(screenSize);
		this.setLayout(new GridBagLayout());
		this.add(selectTeamLabel, gridBagConstraints20);
		this.add(getTeamsScrollPane(), gridBagConstraints21);
	}

	/**
	 * This method initializes teamsTable.
	 * 
	 * @return javax.swing.JTable
	 */
	private JTable getTeamsTable() {
		if (teamsTable == null) {
			teamsTable = new JTable(getSelectTeamTableModel());
			teamsTable.setTableHeader(null);
			teamsTable.getColumnModel().getColumn(0).setCellRenderer(
					new ButtonRenderer());
			teamsTable.getColumnModel().getColumn(0).setCellEditor(
					new ButtonEditor(new JCheckBox()));
			teamsTable.getColumnModel().getColumn(2).setCellRenderer(
					new ButtonRenderer());
			teamsTable.getColumnModel().getColumn(2).setCellEditor(
					new ButtonEditor(new JCheckBox()));
			teamsTable.getColumnModel().getColumn(4).setCellRenderer(
					new ButtonRenderer());
			teamsTable.getColumnModel().getColumn(4).setCellEditor(
					new ButtonEditor(new JCheckBox()));
			teamsTable.setRowHeight(55);
			teamsTable.setFont(new Font("Arial", Font.BOLD, 13));
			teamsTable.setBackground(new Color(238, 238, 238));

			teamsTable.setCellSelectionEnabled(true);
		}
		return teamsTable;
	}

	/**
	 * This method initializes teamsScrollPane.
	 * 
	 * @return javax.swing.JScrollPane
	 */
	private JScrollPane getTeamsScrollPane() {
		if (teamsScrollPane == null) {
			teamsScrollPane = new JScrollPane();
			teamsScrollPane.setPreferredSize(new Dimension(753, 419));
			teamsScrollPane.setViewportView(getTeamsTable());
		}
		return teamsScrollPane;
	}

	/**
	 * This method initializes selectTeamTableModel.
	 * 
	 * @return gui.SelectTeamTableModel
	 */
	private SelectTeamTableModel getSelectTeamTableModel() {
		if (selectTeamTableModel == null) {
			selectTeamTableModel = new SelectTeamTableModel();
		}
		return selectTeamTableModel;
	}

	/**
	 * Sets the main window.
	 * 
	 * @param mainWindow
	 *            the new main window
	 */
	public void setMainWindow(TradecisionbaMainWindow mainWindow) {

		this.mainWindow = mainWindow;
	}

	class ButtonRenderer extends JButton implements TableCellRenderer {

		/**
		 * 
		 */
		private static final long serialVersionUID = -4186121870656168516L;

		public ButtonRenderer() {
			setOpaque(true);
		}

		public Component getTableCellRendererComponent(JTable table,
				Object value, boolean isSelected, boolean hasFocus, int row,
				int col) {
			if (isSelected) {
				setForeground(table.getSelectionForeground());
				setBackground(table.getSelectionBackground());
			} else {
				setForeground(table.getForeground());
				setBackground(UIManager.getColor("Button.background"));
			}
			int index = SelectTeamTableModel.COLUMN_COUNT / 2 * row + col / 2;
			ITeam team = NbaPool.getInstance().getNbaTeams().get(index);
			URL picURL = getClass().getResource(
					"/statistics/logos/" + team.getTeamLogoName());
			ImageIcon icon = new ImageIcon(picURL);
			setIcon(icon);
			return this;
		}
	}

	class ButtonEditor extends DefaultCellEditor {
		/**
		 * 
		 */
		private static final long serialVersionUID = -1549131241616987010L;

		protected JButton button;

		private boolean isPushed;

		public ButtonEditor(JCheckBox checkBox) {
			super(checkBox);
			button = new JButton();
			button.setOpaque(true);
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					fireEditingStopped();
				}
			});
		}

		public Component getTableCellEditorComponent(JTable table,
				Object value, boolean isSelected, int row, int col) {
			if (isSelected) {
				button.setForeground(table.getSelectionForeground());
				button.setBackground(table.getSelectionBackground());
			} else {
				button.setForeground(table.getForeground());
				button.setBackground(table.getBackground());
			}
			int index = SelectTeamTableModel.COLUMN_COUNT / 2 * row + col / 2;
			ITeam team = NbaPool.getInstance().getNbaTeams().get(index);
			URL picURL = getClass().getResource(
					"/statistics/logos/" + team.getTeamLogoName());
			ImageIcon icon = new ImageIcon(picURL);
			SelectTeamActionListener actionListener = mainWindow.new SelectTeamActionListener(
					index);
			button.setIcon(icon);
			button.addActionListener(actionListener);
			isPushed = true;
			return button;
		}

		public Object getCellEditorValue() {
			if (isPushed) {
				new String("");
			}
			isPushed = false;
			return new String("");
		}

		public boolean stopCellEditing() {
			isPushed = false;
			return super.stopCellEditing();
		}

		protected void fireEditingStopped() {
			super.fireEditingStopped();
		}
	}

} // @jve:decl-index=0:visual-constraint="10,10"
